<?php
/*
采购订单列表
*/
declare(strict_types=1);

namespace Modules\Purchasing\Http\Controllers;

use Catch\Base\CatchController as Controller;
use Modules\Purchasing\Models\Purchaseorder;
use Illuminate\Http\Request;
use Modules\Purchasing\Http\Repositories\PurchaseorderReget;
use Catch\Exceptions\FailedException;
use Illuminate\Support\Facades\View;

class PurchaseorderController extends Controller
{
    public function __construct(
        protected readonly Purchaseorder $model
    ){}

    /**列表接口
     * @param Request $request
     * @return mixed
     */
    public function index(Request $request): mixed
    {
        $data = PurchaseorderReget::list($request->all());
        foreach($data as $k=>$v){           
            // $data[$k]['fsupplierid_name'] = $v['fsupplierid_name']==1?'未审核':'未审核'; 
            $data[$k]['fforbidstatus'] = $v['fforbidstatus']==1?'未作废':'作废'; 
        }
        return $data;
    }

    /**弹窗查询列列表接口
     * @param Request $request
     * @return mixed
     */
    public function popupList(Request $request): mixed
    {
        $data = PurchaseorderReget::listTwo($request->all());
        foreach($data as $k=>$v){           
            // $data[$k]['fsupplierid_name'] = $v['fsupplierid_name']==1?'未审核':'未审核'; 
            $data[$k]['fforbidstatus'] = $v['fforbidstatus']==1?'未作废':'作废'; 
            $data[$k]['description'] = $v['state']==1?'已做单':'未做单'; 
        }
        return $data;
    }

    /**
     * @param Request $request
     * @return mixed
     */
    public function store(Request $request)
    {
        $data = $request->all();
        $res = $this->model->where('fid',$data['fid'])->first();
        $fbillno = $this->model->where('fbillno',$data['fbillno'])->first();
        
        if(!empty($fbillno)){
            return throw new FailedException('亲，采购订单号重复无法推送。');
        }
        if(!empty($res)){
            return throw new FailedException('单据内码已存在无法录入请联系技术处理');
        }else{
            return PurchaseorderReget::create($data);
        }
    }

    /**
     * @param $id
     * @return mixed
     */
    public function show($id)
    {
      
        return PurchaseorderReget::find($id);
    }

    /**
     * @param Request $request
     * @param $id
     * @return mixed
     */
    public function update(Request $request)
    {

        return PurchaseorderReget::createUpdate($request->all());
    }

    /**
     * @param $id
     * @return mixed
     */
    public function destroy($id)
    {
       
        return PurchaseorderReget::delete($id);
    }

    




}
